#include "lsm.h"

double LsmLine(double* pb, double xCol[], double yCol[], DWORD n)
{
	double averX = 0.0;
	double averY = 0.0;
	double averXY = 0.0;
	double averX2 = 0.0;
	for (DWORD i = 0; i < n; i++) {
		averX += xCol[i];
		averY += yCol[i];
		averXY += xCol[i] * yCol[i];
		averX2 += xCol[i] * xCol[i];
	}
	averX /= n;
	averY /= n;
	averXY /= n;
	averX2 /= n;
	double k = (averXY - averX * averY) / (averX2 - averX * averX);
	double b = averY - k * averX;

	if (pb != NULL) {
		*pb = b;
	}
	return(k);
}
